﻿using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CucumberRunner.UnitTests{
    [TestClass]
    public class ResultsParserTest{
        private static string ResultSomeFailed(){
            var sb = new StringBuilder();
            sb.AppendLine("Scenario:");
            sb.AppendLine("   ");
            sb.AppendLine("1027 scenarios (1 failed, 1026 passed)");
            sb.AppendLine("8 steps (5 failed, 3 passed)");
            sb.AppendLine("0m0.015s");
            sb.AppendLine("");
            return sb.ToString();
        }

        private static string ResultAllPassed(){
            var sb = new StringBuilder();
            sb.AppendLine("Scenario:");
            sb.AppendLine("   ");
            sb.AppendLine("10 scenarios (10 passed)");
            sb.AppendLine("987 steps (987 passed)");
            sb.AppendLine("0m0.015s");
            sb.AppendLine("");
            return sb.ToString();
        }

        [TestMethod]
        public void ParseReturnsFailedScenariosWhenAllPassed(){
            var resultsParser = new ResultsParser();
            CucumberResults cucumberResults = resultsParser.Parse(ResultAllPassed(), null);
            Assert.AreEqual(cucumberResults.FailedScenarios, 0);
            Assert.AreEqual(cucumberResults.PassedScenarios, 10);
            Assert.AreEqual(cucumberResults.Scenarios, 10);
            Assert.AreEqual(cucumberResults.Steps, 987);
        }

        [TestMethod]
        public void ParseReturnsFailedScenariosWhenSomeFailed(){
            var resultsParser = new ResultsParser();
            CucumberResults cucumberResults = resultsParser.Parse(ResultSomeFailed(), null);
            Assert.AreEqual(cucumberResults.FailedScenarios, 1);
            Assert.AreEqual(cucumberResults.PassedScenarios, 1026);
            Assert.AreEqual(cucumberResults.Scenarios, 1027);
            Assert.AreEqual(cucumberResults.Steps, 8);
        }

        [TestMethod]
        public void ParseReturnsFalseWhenStandardErrorNotNull(){
            var resultsParser = new ResultsParser();
            
            CucumberResults cucumberResults = resultsParser.Parse(ResultAllPassed(), "StandardError");

            Assert.IsFalse(cucumberResults.Passed);
        }

        [TestMethod]
        public void ValidateReturnsFalseWhenOutResultsHaveFailedScenario(){
            var resultsParser = new ResultsParser();
            
            CucumberResults cucumberResults = resultsParser.Parse(ResultSomeFailed(), "StandardError");

            Assert.IsFalse(cucumberResults.Passed);
        }

        [TestMethod]
        public void ValidateReturnsTrueWhenOutputResultsPassedAndErrorIsEmpty(){
            var resultsParser = new ResultsParser();
            
            CucumberResults cucumberResults = resultsParser.Parse(ResultAllPassed(), "");

            Assert.IsTrue(cucumberResults.Passed);
        }

        [TestMethod]
        public void ValidateReturnsTrueWhenOutputResultsPassedAndErrorIsNull(){
            var resultsParser = new ResultsParser();
            
            CucumberResults cucumberResults = resultsParser.Parse(ResultAllPassed(), null);

            Assert.IsTrue(cucumberResults.Passed);
        }
    }
}